package com.googlecode.moneyManager.dao.jdbc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.googlecode.moneyManager.dao.GenericDAO;
import com.googlecode.moneyManager.model.Amount;
import com.googlecode.moneyManager.model.Bank;
import com.googlecode.moneyManager.model.BankAccount;
import com.googlecode.moneyManager.model.Branch;
import com.googlecode.moneyManager.model.Contract;
import com.googlecode.moneyManager.model.Deposit;
import com.googlecode.moneyManager.model.Percentage;
import com.googlecode.moneyManager.model.Customer;
import com.googlecode.moneyManager.model.dto.DepositDTO;
import com.googlecode.moneyManager.service.Builder;

/**
 * @author Sergiy Doroshenko Jun 24, 2008 1:55:26 PM
 *
 **/
@Deprecated
public class DepositDaoJdbc implements GenericDAO<Deposit> {

    // extends JdbcDaoSupport
    // private JdbcTemplate jdbcTemplate;
    private static org.apache.log4j.Logger log = Logger
	    .getLogger(DepositDaoJdbc.class);

    public String create(Deposit d) throws DataAccessException {
	PreparedStatement stm = null;
	StringBuilder q = new StringBuilder("INSERT INTO deposit SET ");
	if (d.getId() == null)
	    d.setId(java.util.UUID.randomUUID().toString());
	try {
	    q.append("layoutDate=?");
	    q.append(",expiredDate=?");
	    q.append(",percentId=?");
	    q.append(",term=?");
	    q.append(",bankAccountId=?");
	    q.append(",contractId=?");
	    q.append(",bankId=?");
	    q.append(",branchId=?");
	    q.append(",userId=?");
	    q.append(",amountId=?");
	    stm = JdbcTemplate.getConn().prepareStatement(q.toString());
	    int i = 1;
	    stm.setLong(i, d.getLayoutDate());
	    i++;
	    stm.setLong(i, d.getExpiredDate());
	    i++;
	    stm.setString(i, d.getPercent().getId().toString());
	    i++;
	    stm.setInt(i, d.getTerm());
	    i++;
//	    stm.setString(i, d.getBankAccount().getId().toString());
//	    i++;
//	    stm.setString(i, d.getContract().getId().toString());
//	    i++;
//	    stm.setString(i, d.getBank().getId().toString());
//	    i++;
//	    stm.setString(i, d.getBranch().getId().toString());
//	    i++;
//	    stm.setString(i, d.getUser().getId().toString());
	    i++;
	    stm.setString(i, d.getAmount().getId().toString());
	    i++;

	    stm.execute();
	    log.debug(stm.toString());
	    log.debug("deposit = " + d.getId() + " created succesfully ");

	} catch (SQLException e) {
	    throw new com.googlecode.moneyManager.exceptions.DataAccessException(stm
		    .toString(), e);
	}
	return d.getId().toString();
    }

    public void delete(Deposit d) throws DataAccessException {
	if (d.getId() == null)
	    throw new com.googlecode.moneyManager.exceptions.DataAccessException(
		    "entity must have id");
	StringBuilder qDep = new StringBuilder("DELETE from deposit where id=?");
	PreparedStatement stm = null;
	try {
	    stm = JdbcTemplate.getConn().prepareStatement(qDep.toString());
	    stm.setString(0, d.getId().toString());
	    stm.execute();
	} catch (SQLException e) {
	    throw new com.googlecode.moneyManager.exceptions.DataAccessException(stm
		    .toString(), e);
	}

    }

    public Deposit get(String id) throws DataAccessException {
	// TODO Auto-generated method stub
	return null;
    }

    public void merge(Deposit t) throws DataAccessException {
	// TODO Auto-generated method stub

    }

    public void saveOrUpdate(Deposit t) throws DataAccessException {
	// TODO Auto-generated method stub

    }

    public List<Deposit> getAll() throws DataAccessException {
	Statement stm = null;
	ResultSet res = null;
	StringBuilder q = new StringBuilder("SELECT * FROM deposit");
	try {
	    stm = JdbcTemplate.getConn().createStatement();
	    res = stm.executeQuery(q.toString());
	} catch (SQLException e) {
	    throw new com.googlecode.moneyManager.exceptions.DataAccessException(stm
		    .toString(), e);
	}
	log.debug(stm.toString());
	try {
	    while (res.next()) {
		DepositDTO depDto = new DepositDTO();
		depDto.id = res.getString(1);
		depDto.layoutDate = res.getLong(2);
		depDto.expiredDate = res.getLong(3);
		depDto.percentId = res.getString(4);
		depDto.bankId = res.getString(5);
		depDto.userId = res.getString(6);
		depDto.amountId = res.getString(7);
	    }
	} catch (SQLException e) {
	    throw new com.googlecode.moneyManager.exceptions.DataAccessException(stm
		    .toString(), e);
	}
	return null;
    }
}
